package com.wcity.manage.service.impl;

import com.wcity.manage.common.utils.AuthUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.mapper.ActMapper;
import com.wcity.manage.dao.model.Act;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.ActService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限服务层
 *
 * @author dy
 */
@Service
public class ActServiceImpl implements ActService {

    @Override
    public List<Act> getNavActList( User user ) {

        List<Integer> authIds = AuthUtil.getAuthIds(user.getRole());
        List<Act> actList = actMapper.findNavByAuthIds( authIds );
        List<Act> navActList = new ArrayList<Act>();

        for ( Act act : actList ) {
            if ( act.getIsNav() && act.getParentId() == 0 ) {
                for( Act temp : actList ) {
                    if ( temp.getParentId() == act.getId() ) act.getChildren().add( temp );
                }
                navActList.add( act );
            }
        }
        return navActList;
    }

    @Override
    public List<Act> getActTreeList() {
        return actMapper.findNavColChildren();
    }

    @Override
    public List<Act> getActListPage( PageModel<Act> page ) {
        return actMapper.findListPage( page );
    }

    @Override
    public String addAct( Act act, String parentId ) {

        if ( actMapper.countByAction( act.getAction() ) > 0 ) return "控制器已存在";
        if ( actMapper.countByAuthId( act.getAuthId() ) > 0 ) return "权限识别ID已存在";
        if ( StringUtil.isNumeric( parentId ) ) {
            act.setParentId( Integer.valueOf( parentId ) );
        }
        try {
            if ( actMapper.insert( act ) <= 0 ) return "添加权限失败";
        } catch ( DataAccessException e ) {
            log.error( "addAct error, {}", e.getMessage(), e );
            return "添加权限失败";
        }
        return "";
    }

    @Override
    public String editAct( Act act, int id, String parentId ) {

        Act localAct = actMapper.find( id );
        if ( !localAct.getAction().equals( act.getAction() ) && actMapper.countByAction( act.getAction() ) > 0 )
            return "控制器已存在";
        if ( localAct.getAuthId() != act.getAuthId() && actMapper.countByAuthId( act.getAuthId() ) > 0 )
            return "权限识别ID已存在";
        if ( StringUtil.isNumeric( parentId ) ) {
            act.setParentId( Integer.valueOf( parentId ) );
        }
        act.setId( id );
        try {
            if ( actMapper.update(act) <= 0 ) return "修改权限失败";
        } catch ( DataAccessException e ) {
            log.error( "editAct error, {}", e.getMessage(), e );
            return "修改权限失败";
        }
        return "";
    }

    @Override
    public Act getAct( int id ) {
        return actMapper.find( id );
    }

    @Resource private ActMapper actMapper;

    private final static Logger log = LoggerFactory.getLogger( ActServiceImpl.class );

}
